package hflyeer.gitee.com.controller;

import hflyeer.gitee.com.entity.Department;
import hflyeer.gitee.com.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;


/**
 * 部门控制器
 */
@Controller("departmentController")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/list")
    public String list(Map<String,Object> map){
        map.put("departmentList",departmentService.getAll());
        return "department_list";
    }

    @GetMapping("/to_add")
    public String toAdd(Map<String,Object> map){
        map.put("department",new Department());
        return "department_add";
    }
    @PostMapping("/add")
    public String add(Department department){
        departmentService.insert(department);
        return "redirect:list";
    }

    @GetMapping(value = "/to_update",params = "sn")
    public String toEdit(String sn,Map<String,Object> map){
        map.put("department",departmentService.get(sn));
        return "department_update";
    }
    @PostMapping("/update")
    public String edit(Department department){
        departmentService.update(department);
        return "redirect:list";
    }

    @GetMapping(value = "/remove",params = "sn")
    public String remove(String sn){
        departmentService.delete(sn);
        return "redirect:list";
    }
}
